#include "MapManagement.h"


CMapManagement::CMapManagement()
{
	currentMap_ = NULL;
}

CMapManagement::~CMapManagement()
{
}

void CMapManagement::readMap(wstring mapName)
{
	if (this->isLoad(mapName))
		this->reloadMap(mapName);
	else
	{
		currentMap_ = new Map;
		currentMap_->mapName_ = mapName;
		this->loadEnemies();
		this->loadTiles();
		loadedMaps_.push_back(new Map(*currentMap_));
	}
}

bool CMapManagement::isLoad(wstring mapName)
{
	for (int i = 0; i < loadedMaps_.size(); i++)
	{
		if (loadedMaps_[i]->mapName_ == mapName)
		{
			return true;
		}
	}
	return false;
}

void CMapManagement::reloadMap(wstring mapName)
{
	for (int i = 0; i < loadedMaps_.size(); i++)
	{
		if (loadedMaps_[i]->mapName_ == mapName)
		{
			memcpy(currentMap_, loadedMaps_[i], sizeof(Map));
			break;
		}
	}
}

void CMapManagement::loadEnemies()
{
	wstring		enemyName;
	D3DXVECTOR3 enemyPosition;
	wfstream	fileStream(currentMap_->mapName_ + L"//Enemies.txt");
	if (fileStream.is_open())
	{
		while (!fileStream.eof())
		{
			fileStream >> enemyName;
			fileStream >> enemyPosition.x >> enemyPosition.y;
			enemyPosition.z = 0.5;
			switch (CConvert::toEnemyName(enemyName))
			{
			case ENEMY_SOLDIER:
				break;
			case ENEMY_CANON_RED:
				currentMap_->vectorEnemies_.push_back(new CCanon_Red());
				break;
			case ENEMY_CANON_WHITE:
				currentMap_->vectorEnemies_.push_back(new CCanon_White(enemyPosition));
				break;
			case ENEMY_BOX:
				currentMap_->vectorEnemies_.push_back(new CBox(enemyPosition));
				break;
			case SOLDIER_STAND_SHOOT:
				currentMap_->vectorEnemies_.push_back(new CStandShoot(enemyPosition));
				break;
			case SOLDIER_WATER_SHOOT:
				currentMap_->vectorEnemies_.push_back(new CWaterShoot(enemyPosition));
				break;
			case SOLDIER_HIDE_SHOOT:
				currentMap_->vectorEnemies_.push_back(new CHideShoot(enemyPosition));
				break;
			case SOLDIER_RUNNING_MAN:
				currentMap_->vectorEnemies_.push_back(new CRunningMan(enemyPosition));

			default:
				break;
			}
		}
	}
}

void CMapManagement::loadTiles()
{
	wstring		tileName;
	D3DXVECTOR3 tilePosition;
	tilePosition.x =  tilePosition.z = 0;
	tilePosition.y = 16;

	wfstream	fileStream(currentMap_->mapName_ + L"//Tiles.txt");
	if (fileStream.is_open())
	{
		while (!fileStream.eof())
		{
			fileStream >> tileName;
			switch (CConvert::toTileName(tileName))
			{
			case TILE:
				currentMap_->vectorTiles_.push_back(new CTile(TILE_RESOURCE(currentMap_->mapName_,tileName), tilePosition));
				break;
			default:
				break;
			}
			if (tileName == L"-1")
			{
				tilePosition.x = 0;
				tilePosition.y += 31;
			}
			else
				tilePosition.x += 31;

		}
	}
}



void CMapManagement::Update(DWORD deltaTime)
{
	for (int i = 0; i < currentMap_->vectorEnemies_.size(); i++)
	{
		currentMap_->vectorEnemies_[i]->Update(deltaTime);
	}
	for (int i = 0; i < currentMap_->vectorTiles_.size(); i++)
	{
		currentMap_->vectorTiles_[i]->Update(deltaTime);
	}
}

void CMapManagement::Render()
{
	for (int i = 0; i < currentMap_->vectorEnemies_.size(); i++)
	{
		currentMap_->vectorEnemies_[i]->Render();
	}
	for (int i = 0; i < currentMap_->vectorTiles_.size(); i++)
	{
		currentMap_->vectorTiles_[i]->Render();
	}
}

void CMapManagement::UpdateAI(D3DXVECTOR3 position)
{
	for (int i = 0; i < currentMap_->vectorEnemies_.size(); i++)
	{
		currentMap_->vectorEnemies_[i]->AI(position);
	}
	
}